package com.ymatou.iapi.shoppingcartservice.testcase;

import java.util.Map;

import org.json.JSONObject;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.shoppingcartservice.parameter.ShoppingcartProductDeleteByUserIdReq;
import com.ymatou.iapi.shoppingcartservice.service.ShoppingcartProductDeleteByUserIdCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.tag.P1;
import com.ymttest.business.service.BuyerShoppingCartService;
import com.ymttest.database.mongo.YmtShoppingCartWapper;
import com.ymttest.utils.EnvSetup;

public class Ts_ShoppingcartProductDeleteByUserId {
	
	private static ShoppingcartProductDeleteByUserIdCall shoppingcartProductDeleteByUserIdCall = new ShoppingcartProductDeleteByUserIdCall();
	private static Integer userId = Integer.valueOf(EnvSetup.getData("shoppingcartUserId"));
	private static BuyerShoppingCartService buyerShoppingCartService = new BuyerShoppingCartService();

	YmtShoppingCartWapper ymtShoppingCartWapper = new YmtShoppingCartWapper();
	
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("根据userId删除购物车商品");
	}
	
	@Before
	public void caseUp() {
	}
	
	@AfterClass
	public static void tearDown() {
		Logger.generateResult(shoppingcartProductDeleteByUserIdCall.getOpurl());
	}	
	
	@After
	public void caseDown() {
		Logger.end();
		try {
			ymtShoppingCartWapper.deleteByUserId(userId);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ShoppingcartProductDeleteByUserId_001() throws Exception {
		Logger.start(true,"根据userid删除购物车商品（默认source为0）");
		try{
			buyerShoppingCartService.getShoppingCartProduct(userId);
			ShoppingcartProductDeleteByUserIdReq req = new ShoppingcartProductDeleteByUserIdReq();
			
			req.setUserId(userId);
			
			JSONObject ret = shoppingcartProductDeleteByUserIdCall.callAndGetReturnData(req);
			int code = ret.getInt("code");
			int count = ymtShoppingCartWapper.getCountByUserId(userId);
			
			Logger.verifyEquals(200, code, "接口code返回异常");
			Logger.verifyEquals(0, count, "mongo数据删除失败");
			
		}catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ShoppingcartProductDeleteByUserId_002() throws Exception {
		Logger.start(true,"根据错误的userid删除购物车商品");
		try{
			Map<String,Object> shoppingCartDB = buyerShoppingCartService.getShoppingCartProduct(userId);
			ShoppingcartProductDeleteByUserIdReq req = new ShoppingcartProductDeleteByUserIdReq();
			
			int source = Integer.parseInt(String.valueOf(shoppingCartDB.get("source")));
			
			req.setUserId(1);
			req.setSource(source);
			
			JSONObject ret = shoppingcartProductDeleteByUserIdCall.callAndGetReturnData(req);
			int code = ret.getInt("code");
			int count = ymtShoppingCartWapper.getCountByUserId(userId);
			
			Logger.verifyEquals(200, code, "接口code返回异常");
			Logger.verifyEquals(1, count, "mongo数据删除失败");
			
		}catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ShoppingcartProductDeleteByUserId_003() throws Exception {
		Logger.start(true,"根据错误的sourceid删除购物车商品");
		try{
			buyerShoppingCartService.getShoppingCartProduct(userId);
			ShoppingcartProductDeleteByUserIdReq req = new ShoppingcartProductDeleteByUserIdReq();
			
			req.setUserId(userId);
			req.setSource(1);
			
			JSONObject ret = shoppingcartProductDeleteByUserIdCall.callAndGetReturnData(req);
			int code = ret.getInt("code");
			int count = ymtShoppingCartWapper.getCountByUserId(userId);
			
			Logger.verifyEquals(200, code, "接口code返回异常");
			Logger.verifyEquals(1, count, "mongo数据删除失败");
			
		}catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ShoppingcartProductDeleteByUserId_004() throws Exception {
		Logger.start(true,"不传入userid");
		try{
			buyerShoppingCartService.getShoppingCartProduct(userId);
			ShoppingcartProductDeleteByUserIdReq req = new ShoppingcartProductDeleteByUserIdReq();
			
			req.setUserId(0);
			req.setSource(1);
			
			JSONObject ret = shoppingcartProductDeleteByUserIdCall.callAndGetReturnData(req);
			int code = ret.getInt("code");
			String message = ret.getString("message");
			
			Logger.verifyEquals(500, code, "接口code返回异常");
			Logger.verifyEquals("userId:must be greater than or equal to 1;", message, "不传入userid提示错误");
			
		}catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ShoppingcartProductDeleteByUserId_005() throws Exception {
		Logger.start(true,"根据source和userid删除购物车商品");
		try{
			int source = 3;
			buyerShoppingCartService.AddShoppingCartProduct(userId, source);
			ShoppingcartProductDeleteByUserIdReq req = new ShoppingcartProductDeleteByUserIdReq();
			
			req.setUserId(userId);
			req.setSource(3);
			
			JSONObject ret = shoppingcartProductDeleteByUserIdCall.callAndGetReturnData(req);
			int code = ret.getInt("code");
			int count = ymtShoppingCartWapper.getCountByUserId(userId);
			
			Logger.verifyEquals(200, code, "接口code返回异常");
			Logger.verifyEquals(0, count, "mongo数据删除失败");
			
		}catch (Exception e) {
			Logger.fail(e);
		}
	}
}
